package Testing;

import DeportManage.DuplicateException;
import DeportManage.NotFormatParcelIDException;
import DeportManage.NotFoundParcelIDException;
import DeportManage.OutRangeOfDaysException;
import DeportManage.Parcel;
import DeportManage.ParcelList;

import org.junit.*;
import static org.junit.Assert.*;

public class ParcelListTest {
	private ParcelList parcel_list;
	private  Parcel parcel_1,parcel_2;
	/**
	 * Initializing the Parcels in the ParcelList
	 *  parcel_1 with "C2034",3,5.4,8.4,5.6,1.9" and parcel_2 with "X2267",9,5.2,3.7,2.1,7.7
	 */
	@Before 
	public void setUp() throws DuplicateException, NotFormatParcelIDException, OutRangeOfDaysException{
		parcel_list = new ParcelList();
		
		parcel_1 = new Parcel("C2034",3,5.4,8.4,5.6,1.9);
		parcel_list.addParcel(parcel_1);
		parcel_2 = new Parcel("X2267",9,5.2,3.7,2.1,7.7);
		parcel_list.addParcel(parcel_2);
	}
	
	@After
	public void tearDown(){
		parcel_list = new ParcelList();
	}
	/**
	 * Test whether the DuplicateException could be thrown out when there is another parcel wit same ID
	 * insert a new Parcel with "C2934",12,9.4,6.4,2.6,8.9, and parcel_1 which is already in Parcel List. 
	 */
	@Test (expected = DuplicateException.class)
	public void testAddParcel() throws DuplicateException, NotFormatParcelIDException, OutRangeOfDaysException{
		// inset a new parcel with different parcel ID : C2934
		parcel_list.addParcel(new Parcel("C2934",12,9.4,6.4,2.6,8.9));
		// inset a new parcel with same parcel ID : C4522
		parcel_list.addParcel(new Parcel("C2034",6,3.9,5.9,10.8,4.1));
	}
	/**
	 * Test whether the NotFoundParcelIDException could be thrown out when the particular parcel cannot be found
	 * insert Parcel IDs of "C2034" and "X2834"
	 */
	@Test (expected = NotFoundParcelIDException.class)
	public void testSearchParcel() throws NotFoundParcelIDException {		
		// search for a parcel with parcel ID: C2034, found in parcel list
		Parcel parcel_a = parcel_list.searchParcelByParcelID("C2034");

		// search for a parcel with parcel ID: X2834, cannot be found in parcel list
		Parcel parcel_b = parcel_list.searchParcelByParcelID("X2834");				
	}
	/**
	 * Test whether the particular parcel could be found, get return from mthod of searchParcelByParcelID()
	 * insert Parcel ID "C2034" to check it. 
	 */
	@Test
	public void testSearchforParcel() throws NotFoundParcelIDException {		
		
		// search for a parcel with parcel ID: C2034, found in parcel list
		Parcel expected_a = parcel_1;
		String message_a = "Fail for parcel-searching: C2034 ";
		Parcel actual_a = parcel_list.searchParcelByParcelID("C2034");
		assertSame(message_a, expected_a, actual_a);
	}
	/**
	 * Test whether the NotFoundParcelIDException could be thrown out when the particular
	 * parcel an application wants to remove cannot be found.
	 * try to remove parcels with Parcel IDs of "C2034" and "X2834"
	 */
	@Test (expected = NotFoundParcelIDException.class)
	public void testRemoveParcel() throws NotFoundParcelIDException {		
		// search for a parcel with parcel ID: C2034, found in parcel list
		parcel_list.removeParcelByParcelID("C2034");

		// search for a parcel with parcel ID: X2834, cannot be found in parcel list
		parcel_list.removeParcelByParcelID("X2834");				
	}

}
